package com.example.dllo.my36kr.news.newsecond.window;


import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.example.dllo.my36kr.R;
import com.example.dllo.my36kr.news.newsecond.SecondActivity;
import com.example.dllo.my36kr.tools.MyApp;
import com.example.dllo.my36kr.tools.NetTool;
import com.example.dllo.my36kr.tools.StaticUrl;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by dllo on 16/8/23.
 */
public class SecondWindow extends  PopupWindow  {

    TextView  totalCountTv , totalViewTv ;
    private final ListView listView;
    private final WindowAdapter adapter;
    Context mContext;


    public SecondWindow( Context context , View.OnClickListener itemsOnClick) {
        super(context);
        this.mContext = context;


        View view = LayoutInflater.from(context).inflate(R.layout.window_second,null);//绑定布局
        listView = (ListView) view.findViewById(R.id.lv_window_second);//listView
        adapter = new WindowAdapter(MyApp.getContext());//adapter

        totalCountTv = (TextView) view.findViewById(R.id.tv_window_second_totalCount);//文章总数
        totalViewTv = (TextView) view.findViewById(R.id.tv_window_second_totalView);//总浏览量



        this.setContentView(view);
        this.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LinearLayout.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        ColorDrawable dw = new ColorDrawable(0xb0000000);//设置窗口的颜色
        this.setBackgroundDrawable(dw);
        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                dismiss();
                return false;
            }
        });

    }
    public void setA(String s){//从 secondActivity中获取feedId
        final NetTool mNetTool = new NetTool();
        mNetTool.getNetData(StaticUrl.NEWS_AUTHOR_URL + s + "/author-region", WindowBean.class, new NetTool.NetListener<WindowBean>() {
            @Override
            public void onSuccess(final WindowBean windowBean) {
                adapter.setBean(windowBean);
                listView.setAdapter(adapter);
                totalCountTv.setText(windowBean.getData().getTotalCount() + "篇");
                totalViewTv.setText((windowBean.getData().getTotalView() / 10000) + "万");
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Intent intent1 = new Intent(mContext,SecondActivity.class);
                        intent1.putExtra("details",String.valueOf(windowBean.getData().getLatestArticle().get(position).getPostId()));
                        mContext.startActivity(intent1);

                    }
                });
            }
            @Override
            public void onError(String errorMsg) {

            }
        });

    }

}
